﻿using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting; 
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System.Text;
using Autofac; 
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.Extensions.Logging;
using Microsoft.IdentityModel.Tokens;
using Repository.install;
using Service.install;  

namespace WebAPI
{
    public class Startup
    {
  
        public Startup(IHostingEnvironment env)
        {
            var builder = new ConfigurationBuilder()
                .SetBasePath(env.ContentRootPath)
                .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                .AddEnvironmentVariables();
            this.Configuration = builder.Build();
        }

        public IConfigurationRoot Configuration { get; private set; }

        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseHsts();
            }
            app.UseMvc();
            loggerFactory.AddConsole(Configuration.GetSection("Logging"));
            loggerFactory.AddDebug();
            app.UseHttpsRedirection();
            app.UseAuthentication();//注意添加这一句，启用jwt验证           
            app.UseCors("AllowSpecificOrigin");
        }

        public void ConfigureContainer(ContainerBuilder builder)
        {
            builder.RegisterModule(new AutofacModule());
            builder.RegisterModule(new RepositoryModule());
        }

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddCors(options =>
                    {
                        options.AddPolicy("AllowSpecificOrigin",
                            builder =>
                            {
                                builder.WithOrigins("http://39.105.62.222:80").AllowAnyHeader();
                                builder.WithOrigins("http://127.0.0.1:9528").AllowAnyHeader();
                            });
                    });
            services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
                .AddJwtBearer(options =>
                {
                    options.TokenValidationParameters = new TokenValidationParameters
                    {
                        ValidateIssuer = true,//是否验证Issuer
                        ValidateAudience = true,//是否验证Audience
                        ValidateLifetime = true,//是否验证失效时间
                        ValidateIssuerSigningKey = true,//是否验证SecurityKey
                        ValidAudience = "xww_web",//Audience
                        ValidIssuer = "xww_web",//Issuer，这两项和前面签发jwt的设置一致
                        IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["SecurityKey"]))//拿到SecurityKey
                    };
                });
            services.AddMvc();
        }
     
    }
}

//public static IWebHost BuildWebHost(string[] args)
//{
//var config = new ConfigurationBuilder()
//    .AddCommandLine(args)
//    .Build();
//string ip = config["ip"];
//string port = config["port"];

//    return WebHost.CreateDefaultBuilder(args)
//.UseStartup<Startup>()
//.UseUrls($"http://{ip}:{port}")
//.Build();
//}

//绑定WebSocket
//app.Map("/wsone/Connect", con =>
//{
//    con.UseWebSockets();
//    WSHanleTwo _two = new WSHanleTwo();
//    con.Use(_two.Connect);
//});